package com.yun.zf.admin.quartz.controller;

import com.yun.zf.admin.quartz.service.ISysJobService;
import com.yun.zf.common.admin.domain.SysJob;
import com.yun.zf.common.annotation.Log;
import com.yun.zf.admin.controller.BaseController;
import com.yun.zf.common.enums.BusinessType;
import com.yun.zf.common.utils.ExcelUtil;
import com.yun.zf.common.vo.Response;
import com.yun.zf.system.exception.TaskException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 调度任务信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/job")
@Api(value = "",tags = "定时任务")
public class SysJobController extends BaseController
{
    @Autowired
    private ISysJobService jobService;

    /**
     * 查询定时任务列表
     */
/*    @PreAuthorize("@ss.hasPermi('monitor:job:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysJob sysJob)
    {
        startPage();
        List<SysJob> list = jobService.selectJobList(sysJob);
        return getDataTable(list);
    }*/

    /**
     * 导出定时任务列表
     */
    @Log(title = "定时任务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public Response export(SysJob sysJob)
    {
        List<SysJob> list = jobService.selectJobList(sysJob);
        ExcelUtil<SysJob> util = new ExcelUtil<SysJob>(SysJob.class);
        return util.exportExcel(list, "定时任务");
    }

    /**
     * 获取定时任务详细信息
     */
    @GetMapping(value = "/{jobId}")
    @ApiOperation(value = "获取定时任务详细信息",notes = "获取定时任务详细信息")
    public Response getInfo(@PathVariable("jobId") Long jobId)
    {
        return Response.success(jobService.selectJobById(jobId));
    }

    @Log(title = "定时任务", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增定时任务")
    public Response add(@RequestBody SysJob sysJob) throws SchedulerException, TaskException
    {
        return toAjax(jobService.insertJob(sysJob));
    }

    /**
     * 修改定时任务
     */
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public Response edit(@RequestBody SysJob sysJob) throws SchedulerException, TaskException
    {
        return toAjax(jobService.updateJob(sysJob));
    }

    /**
     * 定时任务状态修改
     */
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public Response changeStatus(@RequestBody SysJob job) throws SchedulerException
    {
        SysJob newJob = jobService.selectJobById(job.getJobId());
        newJob.setStatus(job.getStatus());
        return toAjax(jobService.changeStatus(newJob));
    }

    /**
     * 定时任务立即执行一次
     */
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/run")
    @ApiOperation(value = "定时任务立即执行一次",notes = "定时任务立即执行一次")
    public Response run(@RequestBody SysJob job) throws SchedulerException
    {
        jobService.run(job);
        return Response.success();
    }

    /**
     * 删除定时任务
     */
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobIds}")
    public Response remove(@PathVariable Long[] jobIds) throws SchedulerException, TaskException
    {
        jobService.deleteJobByIds(jobIds);
        return Response.success();
    }
}
